# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@com_google_googleapis_imports//:imports.bzl", "cc_proto_library")
load("//p4_pdpi/testing:diff_test.bzl", "cmd_diff_test")

package(
    default_testonly = True,
    default_visibility = ["//visibility:public"],
    licenses = ["notice"],
)

cc_library(
    name = "dataplane_validation",
    srcs = ["dataplane_validation.cc"],
    hdrs = ["dataplane_validation.h"],
    deps = [
        ":output_writer",
        ":packet_injection",
        ":packet_trace_cc_proto",
        ":port_id_map",
        ":switch_api",
        ":test_run_validation",
        ":test_vector",
        ":test_vector_cc_proto",
        ":user_provided_packet_test_vector",
        ":validation_result",
        "//dvaas/thinkit_tests:dvaas_regression_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:proto_ordering",
        "//gutil/gutil:status",
        "//gutil/gutil:test_artifact_writer",
        "//gutil/gutil:version",
        "//lib/gnmi:gnmi_helper",
        "//lib/gnmi:openconfig_cc_proto",
        "//lib/p4rt:p4rt_port",
        "//p4_pdpi:ir",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi:p4_runtime_session",
        "//p4_pdpi:p4_runtime_session_extras",
        "//p4_pdpi:sequencing",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "//p4_pdpi/string_encodings:hex_string",
        "//p4_symbolic/packet_synthesizer:coverage_goal_cc_proto",
        "//p4_symbolic/packet_synthesizer:packet_synthesizer_cc_proto",
        "//sai_p4/instantiations/google:p4_versions",
        "//tests/lib:switch_test_setup_helpers",
        "//thinkit:mirror_testbed",
        "@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
        "@com_github_gnmi//proto/gnmi:gnmi_cc_proto",
        "@com_github_google_glog//:glog",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
    ],
)

proto_library(
    name = "dvaas_detective_proto",
    srcs = ["dvaas_detective.proto"],
)

cc_proto_library(
    name = "dvaas_detective_cc_proto",
    deps = [":dvaas_detective_proto"],
)

cc_library(
    name = "dvaas_detective",
    srcs = ["dvaas_detective.cc"],
    hdrs = ["dvaas_detective.h"],
    deps = [
        ":dvaas_detective_cc_proto",
        ":test_vector_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:overload",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:variant",
        "@com_google_ydf//yggdrasil_decision_forests/dataset:data_spec_cc_proto",
        "@com_google_ydf//yggdrasil_decision_forests/model/decision_tree",
        "@com_google_ydf//yggdrasil_decision_forests/model/decision_tree:decision_tree_cc_proto",
        "@com_google_ydf//yggdrasil_decision_forests/model/random_forest",
        "@com_google_ydf//yggdrasil_decision_forests/utils:distribution_cc_proto",
    ],
)

# go/golden-test-with-coverage
cc_test(
    name = "dvaas_detective_test",
    srcs = ["dvaas_detective_test.cc"],
    linkstatic = True,
    deps = [
        ":dvaas_detective",
        ":dvaas_detective_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:testing",
        ":test_vector_cc_proto",
        "//gutil/gutil:status_matchers",
        "//p4_pdpi/internal:ordered_map",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_googletest//:gtest_main",
        "@com_google_ydf//yggdrasil_decision_forests/dataset:data_spec_cc_proto",
        "@com_google_ydf//yggdrasil_decision_forests/model/decision_tree",
        "@com_google_ydf//yggdrasil_decision_forests/model/random_forest",
    ],
)

cmd_diff_test(
    name = "dvaas_detective_diff_test",
    actual_cmd = " | ".join([
        "$(execpath :dvaas_detective_test)",
        # Strip unnecessary lines for golden testing.
        "sed '1,/^\\[ RUN/d'",  # Strip everything up to a line beginning with '[ RUN'.
        "sed '/^\\[/d'",  # Strip every line beginning with '['.
    ]),
    expected = "dvaas_detective_test.expected",
    tools = [":dvaas_detective_test"],
)

cc_test(
    name = "dataplane_validation_test",
    srcs = ["dataplane_validation_test.cc"],
    linkstatic = True,
    deps = [
        ":dataplane_validation",
        ":packet_trace_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:test_artifact_writer",
        "//gutil/gutil:testing",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest_main",
    ],
)

cmd_diff_test(
    name = "dataplane_validation_diff_test",
    actual_cmd = " | ".join([
        "$(execpath :dataplane_validation_test)",
        # Strip unnecessary lines for golden testing.
        "sed '1,/^\\[ RUN/d'",  # Strip everything up to a line beginning with '[ RUN'.
        "sed '/^\\[/d'",  # Strip every line beginning with '['.
    ]),
    expected = "dataplane_validation_test.expected",
    tools = [":dataplane_validation_test"],
)

cc_library(
    name = "validation_result",
    srcs = ["validation_result.cc"],
    hdrs = ["validation_result.h"],
    deps = [
        ":test_run_validation",
        ":test_vector_cc_proto",
        ":test_vector_stats",
        "//gutil/gutil:status",
        "//p4_symbolic/packet_synthesizer:packet_synthesizer_cc_proto",
        "@com_github_google_glog//:glog",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library(
    name = "output_writer",
    hdrs = ["output_writer.h"],
    deps = [
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "packet_injection",
    srcs = ["packet_injection.cc"],
    hdrs = ["packet_injection.h"],
    deps = [
        ":port_id_map",
        ":test_vector",
        ":test_vector_cc_proto",
        "//gutil/gutil:status",
        "//lib/p4rt:p4rt_port",
        "//p4_pdpi:ir",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi:p4_runtime_session",
        "//p4_pdpi/packetlib",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
	"//p4_pdpi/utils:ir",
        "//tests/forwarding:util",
        "@com_github_google_glog//:glog",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
)
cc_library(
    name = "test_run_validation",
    srcs = ["test_run_validation.cc"],
    hdrs = ["test_run_validation.h"],
    deps = [
        ":switch_api",
        ":test_vector_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:proto_ordering",
        "//gutil/gutil:status_matchers",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_github_google_glog//:glog",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:optional",
        "@com_google_googletest//:gtest",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "arriba_test_vector_validation",
    testonly = True,
    srcs = ["arriba_test_vector_validation.cc"],
    hdrs = ["arriba_test_vector_validation.h"],
    deps = [
        ":packet_injection",
        ":port_id_map",
	":test_insights",
        ":test_run_validation",
        ":test_vector",
        ":test_vector_cc_proto",
        ":validation_result",
        "//gutil/gutil:proto",
        "//gutil/gutil:test_artifact_writer",
        "//p4_pdpi:ir",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi:p4_runtime_session",
        "//p4_pdpi:p4_runtime_session_extras",
        "//sai_p4/instantiations/google/test_tools:test_entries",
        "//tests/lib:switch_test_setup_helpers",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
    ],
)

cc_library(
    name = "mirror_testbed_config",
    srcs = ["mirror_testbed_config.cc"],
    hdrs = ["mirror_testbed_config.h"],
    deps = [
        ":switch_api",
        "//lib/gnmi:gnmi_helper",
        "//lib/gnmi:openconfig_cc_proto",
        "//lib/p4rt:p4rt_port",
        "//p4_pdpi:ir",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi:p4_runtime_session",
        "//tests/lib:switch_test_setup_helpers",
        "//thinkit:mirror_testbed",
        "//thinkit:mirror_testbed_fixture",
        "@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
        "@com_github_google_glog//:glog",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

proto_library(
    name = "packet_trace_proto",
    testonly = False,
    srcs = ["packet_trace.proto"],
    deps = ["//p4_pdpi:ir_proto"],
)

cc_proto_library(
    name = "packet_trace_cc_proto",
    testonly = False,
    deps = [":packet_trace_proto"],
)

proto_library(
    name = "test_vector_proto",
    srcs = ["test_vector.proto"],
    deps = [
        ":packet_trace_proto",
        "//p4_pdpi:ir_proto",
        "//p4_pdpi/packetlib:packetlib_proto",
    ],
)

cc_proto_library(
    name = "test_vector_cc_proto",
    deps = [":test_vector_proto"],
)

cc_library(
    name = "test_vector",
    testonly = True,
    srcs = ["test_vector.cc"],
    hdrs = ["test_vector.h"],
    deps = [
        ":test_vector_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi/packetlib",
        "//p4_pdpi/packetlib:bit_widths",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
	"//p4_pdpi/string_encodings:hex_string",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_googlesource_code_re2//:re2",
    ],
)

cc_test(
    name = "test_vector_test",
    srcs = ["test_vector_test.cc"],
    deps = [
        ":test_vector",
        ":test_vector_cc_proto",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/packetlib",
        "//p4_pdpi/packetlib:bit_widths",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "test_vector_stats",
    srcs = ["test_vector_stats.cc"],
    hdrs = ["test_vector_stats.h"],
    deps = [
        ":test_vector_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library(
    name = "switch_api",
    hdrs = ["switch_api.h"],
    deps = ["//p4_pdpi:p4_runtime_session"],
)

cc_library(
    name = "port_id_map",
    srcs = ["port_id_map.cc"],
    hdrs = ["port_id_map.h"],
    deps = [
        "//gutil/gutil:status",
        "//gutil/gutil:test_artifact_writer",
        "//lib/gnmi:gnmi_helper",
        "//lib/p4rt:p4rt_port",
        "@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
    ],
)


cc_test(
    name = "port_id_map_test",
    srcs = ["port_id_map_test.cc"],
    deps = [
        ":port_id_map",
        "//gutil/gutil:status_matchers",
        "//lib/p4rt:p4rt_port",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_googletest//:gtest_main",
    ],
)


cc_library(
    name = "user_provided_packet_test_vector",
    testonly = True,
    srcs = ["user_provided_packet_test_vector.cc"],
    hdrs = ["user_provided_packet_test_vector.h"],
    deps = [
        ":test_vector",
        ":test_vector_cc_proto",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "//p4_pdpi:ir",
        "//p4_pdpi/packetlib",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "traffic_generator",
    srcs = ["traffic_generator.cc"],
    hdrs = ["traffic_generator.h"],
    deps = [
        ":dataplane_validation",
        ":mirror_testbed_config",
        ":packet_injection",
        ":port_id_map",
        ":switch_api",
        ":test_run_validation",
        ":test_vector",
        ":test_vector_cc_proto",
        ":validation_result",
        "//gutil/gutil:proto",
        "//gutil/gutil:test_artifact_writer",
        "//lib/p4rt:p4rt_port",
        "//p4_pdpi:ir",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi:p4_runtime_session",
        "//p4_pdpi:p4_runtime_session_extras",
        "//p4_symbolic/packet_synthesizer:packet_synthesizer_cc_proto",
        "//tests/forwarding:util",
        "//thinkit:mirror_testbed",
        "@com_github_google_glog//:glog",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
    ],
)

cc_library(
    name = "test_insights",
    testonly = True,
    srcs = ["test_insights.cc"],
    hdrs = ["test_insights.h"],
    deps = [
        ":packet_trace_cc_proto",
        ":test_vector",
        ":test_vector_cc_proto",
        "//gutil/gutil:proto",
        "//p4_pdpi:ir_cc_proto",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

# go/golden-test-with-coverage
cc_test(
    name = "user_provided_packet_test_vector_test",
    srcs = ["user_provided_packet_test_vector_test.cc"],
    linkstatic = True,
    deps = [
        ":test_vector",
        ":test_vector_cc_proto",
        ":user_provided_packet_test_vector",
        "//gutil/gutil:collections",
        "//gutil/gutil:proto",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/packetlib",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "//p4_pdpi/testing:test_p4info_cc",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest_main",
        "@com_google_protobuf//:protobuf",
    ],
)

cmd_diff_test(
    name = "user_provided_packet_test_vector_diff_test",
    actual_cmd = " | ".join([
        "$(execpath :user_provided_packet_test_vector_test)",
        # Strip unnecessary lines for golden testing.
        "sed '1,/^\\[ RUN/d'",  # Strip everything up to a line beginning with '[ RUN'.
        "sed '/^\\[/d'",  # Strip every line beginning with '['.
    ]),
    expected = "user_provided_packet_test_vector_test.expected",
    tools = [":user_provided_packet_test_vector_test"],
)

# go/golden-test-with-coverage
cc_test(
    name = "test_vector_stats_test",
    srcs = ["test_vector_stats_test.cc"],
    linkstatic = True,
    deps = [
        ":test_vector_cc_proto",
        ":test_vector_stats",
        "//gutil/gutil:testing",
        "@com_google_googletest//:gtest_main",
    ],
)
 
cmd_diff_test(
    name = "test_vector_stats_diff_test",
    actual_cmd = " | ".join([
        "$(execpath :test_vector_stats_test)",
        # Strip unnecessary lines for golden testing.
        "sed '1,/^\\[ RUN/d'",  # Strip everything up to a line beginning with '[ RUN'.
        "sed '/^\\[/d'",  # Strip every line beginning with '['.
    ]),
    expected = "test_vector_stats_test.expected",
    tools = [":test_vector_stats_test"],
)

# go/golden-test-with-coverage
cc_test(
    name = "test_run_validation_test_runner",
    srcs = ["test_run_validation_test_runner.cc"],
    linkstatic = True,
    deps = [
        ":test_run_validation",
        ":test_vector_cc_proto",
        "//gutil/gutil:testing",
        "//p4_pdpi:ir_cc_proto",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_github_google_glog//:glog",
        "@com_google_absl//absl/status:statusor",
    ],
)

cmd_diff_test(
    name = "test_run_validation_golden_test",
    actual_cmd = "$(execpath :test_run_validation_test_runner)",
    expected = ":test_run_validation_test.expected.output",
    tools = [":test_run_validation_test_runner"],
)

cc_test(
    name = "test_run_validation_test",
    srcs = ["test_run_validation_test.cc"],
    deps = [
        ":switch_api",
        ":test_run_validation",
        ":test_vector_cc_proto",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest_main",
        "@com_google_protobuf//:protobuf",
    ],
)
